package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.*;
import com.wn.mapper.TourGuideMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class TourGuideService {
    @Autowired
    private TourGuideMapper tourGuideMapper;

    //查询所有
    public PageInfo<TourGuide> queryAll(PageParam<TourGuide> pageParam) {


        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //分页
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<TourGuide> tourGuideList = tourGuideMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<TourGuide> tourGuidePageInfo = new PageInfo<>(tourGuideList);
            return tourGuidePageInfo;
        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<TourGuide> tourGuideList = tourGuideMapper.queryAll();
            PageInfo<TourGuide> tourGuidePageInfo = new PageInfo<>(tourGuideList);
            return tourGuidePageInfo;
        }
    }

    //根据id查询
    public Rsp gettourGuideById(Integer id) {
        TourGuide tourGuide = tourGuideMapper.selectByPrimaryKey(id);
        if (StringUtils.isEmpty(tourGuide)) {
            return new Rsp().msg("查询失败！");
        } else {
            return new Rsp().success(tourGuide);
        }
    }

    //逻辑删除
    public Rsp deleteById(TourGuide tourGuide) {
        int i = tourGuideMapper.deleteByPrimaryKey(tourGuide);
        if (i==1) {
            return new Rsp().success(i);
        } else {
            return new Rsp().msg("删除失败！");
        }
    }
    //增加导游
    public Rsp insertTourGuid(TourGuide tourGuide){
        int insert = tourGuideMapper.insertSelective(tourGuide);
        if(insert == 1){
            return new Rsp().success(insert);
        }else {
            return new Rsp().fail().msg("添加失败！");
        }
    }

    //动态修改导游
    public Rsp updateTourGuide(TourGuide tourGuide){
        int i = tourGuideMapper.updateByPrimaryKeySelective(tourGuide);
        if(i==1){
            return new Rsp().success(i);
        }else {
            return new Rsp().msg("修改失败");
        }
    }

    //修改导游的状态
    public Rsp updateTourGuideStatus(TourGuide tourGuide){
        if(tourGuide.getTourGuideStatus().equals("1")){
            tourGuide.setTourGuideStatus("0");
        }else {
            tourGuide.setTourGuideStatus("1");
        }
        Integer updateTourGuideStatus = tourGuideMapper.updateTourGuideStatus(tourGuide);
        if(updateTourGuideStatus.equals(null)){
            return new Rsp().fail().msg("修改失败！");
        }else{
            return new Rsp().success(updateTourGuideStatus);
        }
    }

}


